package API.DAY11.reflect;

import API.DAY10.reflect.Person;

import java.lang.reflect.Method;
import java.util.Scanner;

public class ReflectDemo4 {
    public static void main(String[] args) throws Exception{
        Person p = new Person();
        p.sayHello();
        System.out.println(p.getName());

        Class cls = Class.forName("API.DAY10.reflect.Person");
        Object obj = cls.newInstance();

        Method method = cls.getMethod("sayHello");
        method.invoke(obj);
        Method method1 = cls.getMethod("getName");
        System.out.println(method1.invoke(obj));

        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入包名");
        String packageName= scanner.nextLine();
        System.out.println("请输入方法名");
        String methodName = scanner.nextLine();

        Class cls2 = Class.forName(packageName);
        Object obj2 = cls2.newInstance();

        Method method2 = cls2.getMethod(methodName);
        method2.invoke(obj2);
    }
}
